﻿#region Header

/***************************************************************************
Author : Krishan Kaliraman (www.sharedwith.me)  Date: 1 Aug 2012
***************************************************************************/

#endregion Header
using System.Collections.Generic;
using System.Text;

namespace Common
{
    public class TeamMember
    {
        #region Properties

        public string MemberName { get; set; }
        public string MemberDisplayName { get; set; }
        public string MemberRemainingWorkItems { get; set; }
        public List<Activity> TemMemberActivities = new List<Activity>();
        public List<Activity> TemMemberCurrentCommittedActivities = new List<Activity>();

        # endregion

        # region Constructor

        //public TeamMember(IEnumerable<WorkItem> workItems,string memberName,string memberDisplayName)
        //{
        //    MemberName = memberName;
        //    MemberDisplayrName = memberDisplayName;
        //    FillTeamMemberActivities(workItems);
        //    FillTeamMemberCurrentCommittedActivities(workItems);
        //    FillTeamMemberRemainingWorkItems();
        //}

        public TeamMember(IEnumerable<Activity> workItems, string memberName, string memberDisplayName)
        {
            MemberName = memberName;
            MemberDisplayName = memberDisplayName;
            FillTeamMemberActivities(workItems);
            FillTeamMemberCurrentSprintCommittedActivities(StaticItems.TotalWorkItemsInPipeLineForCurrentSprint);
            FillTeamMemberRemainingWorkItems();
        } 

        # endregion

        # region PrivateMethods

        private void FillTeamMemberActivities(IEnumerable<Activity> workItems)
        {
            foreach (Activity currentActivity in workItems)
            {
                bool isActivityDoneOrWorkInProgress = CheckCurrentActivityIsDoneOrInProgress(currentActivity.State);
                if (MemberName == currentActivity.AssignedTo && currentActivity.WorkItemType == "Task" && isActivityDoneOrWorkInProgress)
                {
                    TemMemberActivities.Add(currentActivity);
                }
            }
        }

        private bool CheckCurrentActivityIsDoneOrInProgress(string activityState)
        {
            bool isActivityDoneOrWorkInProgress = activityState=="Done" || activityState=="In Progress";
            return isActivityDoneOrWorkInProgress;
        }
        private void FillTeamMemberCurrentSprintCommittedActivities(IEnumerable<Activity> currentSprintRemainingActivities)
        {
            foreach (Activity currentActivity in currentSprintRemainingActivities)
            {
                bool isActivityDoneOrWorkInProgress = CheckCurrentActivityIsTODoOrInProgress(currentActivity.State);
                if (MemberName == currentActivity.AssignedTo && currentActivity.WorkItemType == "Task" && isActivityDoneOrWorkInProgress)
                {
                    TemMemberCurrentCommittedActivities.Add(currentActivity);
                }
            }           
        }

        private bool CheckCurrentActivityIsTODoOrInProgress(string activityState)
        {
            bool isActivityDoneOrWorkInProgress = activityState == "To Do" || activityState == "In Progress";
            return isActivityDoneOrWorkInProgress;
        }
        private void FillTeamMemberRemainingWorkItems()
        {
            StringBuilder remainingItems = new StringBuilder();
            remainingItems.AppendFormat("{0} Pipelined Work({1} items)-->", StaticItems.CurrentSprintIteration, TemMemberCurrentCommittedActivities.Count.ToString());
            foreach (Activity  currentActivity in TemMemberCurrentCommittedActivities)
            {
                remainingItems.AppendFormat("Task Id: {0} - {1}({2}) ", currentActivity.Id, currentActivity.Title, currentActivity.State);
                remainingItems.AppendLine();
            }            
            MemberRemainingWorkItems = remainingItems.ToString();
        }

        #endregion
    }
}
 